package com.jms.tomcat.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

public class JmsTomcatConfig implements TomcatConfig{

    public static final String PROTYPE = "properties";
    public static final String XMLTYPE = "xml";
    private final Properties config;

    public JmsTomcatConfig(String configFileName, String type) throws IOException {
        config = new Properties();
        InputStream inputStream = null;
        if (PROTYPE.equalsIgnoreCase(type)) {
            inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(String.format("%s.properties", configFileName));
            config.load(inputStream);
        } else if (XMLTYPE.equalsIgnoreCase(type)) {
            inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(String.format("%s.xml", configFileName));
            config.loadFromXML(inputStream);
        }
        if (inputStream != null) {
            inputStream.close();
        }
    }

    public JmsTomcatConfig(Properties properties) {
        config = new Properties();
        config.putAll(properties);
    }

    public JmsTomcatConfig(Map<String, Object> map) {
        config = new Properties();
        config.putAll(map);
    }

    @Override
    public Object get(String key) {
        return config.get(key);
    }

    @Override
    public String getString(String key) {
        return config.getProperty(key);
    }

    @Override
    public Integer getIntegerValue(String key) {
        if (config.containsKey(key)) {
            return Integer.parseInt(config.getProperty(key));
        }
        return null;
    }
}
